package com.masterclient.app.businessmodule.usermodule.login.contract;

import android.text.TextUtils;

import com.eoner.baselib.presenter.FCBaseCallBack;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.businessmodule.usermodule.api.UserHttpService;
import com.masterclient.app.businessmodule.usermodule.login.beans.AuthBindNewBean;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviteInfoBean;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginBgBean;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginResultBean;
import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/07/12
*/
/**
 * Created by TMVPHelper on 2017/07/12
 */
public class NewQuickLoginPresenter extends NewQuickLoginContract.Presenter {

    @Override
    public void authLogin(String auth_type, String head_url, String nickname, String union_id, String uid, String inviter_code) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("auth_type", auth_type);
            jsonObject.put("head_url", head_url);
            jsonObject.put("nick_name", nickname);
            jsonObject.put("union_id", union_id);
            jsonObject.put("uid", uid);
            jsonObject.put("invite_code", inviter_code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<LoginResultBean> observable = UserHttpService.getInstance().authLogin(body);
        Disposable disposable = toSubscribe(observable, new Consumer<LoginResultBean>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull LoginResultBean bean) throws Exception {

                if ("0".equals(bean.getCode())) {
                    mView.authLoginData(bean.getData());
                } else {
                    mView.authLoginError(bean.getMsg(), bean.getData() == null ||
                            TextUtils.isEmpty(bean.getData().getS_fail_reason()) ? bean.getMsg() : bean.getData().getS_fail_reason());
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.authLoginError(o.getMessage(), null);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getInviteInfo(String invite_code) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("invite_code", invite_code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<InviteInfoBean> observable = UserHttpService.getInstance().getInviteInfo(body);
        Disposable disposable = toSubscribe(observable, new Consumer<InviteInfoBean>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull InviteInfoBean bean) throws Exception {

                if ("0".equals(bean.getCode())) {
                    mView.showInviteInfo(bean.getData());
                } else {
                    mView.showInviteInfoFail(bean.getMsg());
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showInviteInfoFail(o.getMessage());
            }
        });
        mRxManager.add(disposable);

    }

    @Override
    public void authBind(String auth_type, String mobile, String union_id, String invite_id, String uid, String nick_name, String headimgurl, String nonce, String customer_id, String reward_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("auth_type", auth_type);
            jsonObject.put("mobile", mobile);
            jsonObject.put("union_id", union_id);
            jsonObject.put("invite_code", invite_id);
            jsonObject.put("uid", uid);
            jsonObject.put("nick_name", nick_name);
            jsonObject.put("headimgurl", headimgurl);
            jsonObject.put("nonce", nonce);
            jsonObject.put("customer_id", customer_id);
            jsonObject.put("reward_id", reward_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<AuthBindNewBean> observable = UserHttpService.getInstance().authBind(body);
        Disposable disposable = toSubscribe(observable, new Consumer<AuthBindNewBean>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull AuthBindNewBean bean) throws Exception {

                if ("0".equals(bean.getCode())) {
                    mView.authBindSuccess(bean.getCode(), bean.getData(), bean.getMsg(), invite_id);
                } else if ("30000".equals(bean.getCode())) {
                    mView.authBindSuccess(bean.getCode(), bean.getData(), bean.getMsg(), invite_id);
                } else {
                    mView.authBindFailded(bean.getMsg());
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.authBindFailded(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getAdInfo(String id) {

        mRxManager.add(toNewSubscribe(UserHttpService.getInstance().getAdInfo(id), new FCBaseCallBack<CommonBaseResponse<LoginBgBean>>() {
            @Override
            public void onSuccess(CommonBaseResponse<LoginBgBean> bean) {
                mView.getAdInfoSuccess(bean.getData());
            }
        }));
    }
}